//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class PrivateMessagesSentItemsControl : BaseNopFrontendUserControl
    {
        protected override void OnPreRender(EventArgs e) {
            if (gvSent.Rows.Count > 0) {
                btnDeleteSelected.Visible = true;
            } else {
                btnDeleteSelected.Visible = false;
            }
            base.OnPreRender(e);
        }

        protected string GetToInfo(int customerId) {
            string customerInfo = string.Empty;
            var customer = this.CustomerService.GetCustomerById(customerId);
            if (customer != null && !customer.IsGuest) {
                if (this.CustomerService.AllowViewingProfiles) {
                    customerInfo = string.Format("<a href=\"{0}\">{1}</a>", SEOHelper.GetUserProfileUrl(customer.CustomerId), Server.HtmlEncode(customer.FormatUserName()));
                } else {
                    customerInfo = Server.HtmlEncode(customer.FormatUserName());
                }
            }
            return customerInfo;
        }

        protected string GetSubjectInfo(PrivateMessage pm) {
            string result = string.Empty;
            string subjectInfo = Server.HtmlEncode(pm.Subject);
            result = string.Format("<a href=\"{0}viewpm.aspx?pm={1}\">{2}</a>", CommonHelper.GetStoreLocation(), pm.PrivateMessageId, subjectInfo);
            return result;
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e) {
            if (Page.IsValid) {
                try {
                    foreach (GridViewRow row in gvSent.Rows) {
                        var cbSelect = row.FindControl("cbSelect") as CheckBox;
                        var hfPrivateMessageId = row.FindControl("hfPrivateMessageId") as HiddenField;
                        if (cbSelect != null && hfPrivateMessageId != null) {
                            bool selected = cbSelect.Checked;
                            int pmId = int.Parse(hfPrivateMessageId.Value);
                            if (selected) {
                                var pm = this.ForumService.GetPrivateMessageById(pmId);
                                if (pm != null) {
                                    if (pm.FromUserId == NopContext.Current.User.CustomerId) {
                                        pm.IsDeletedByAuthor = true;
                                        this.ForumService.UpdatePrivateMessage(pm);
                                    }
                                }
                            }
                        }
                    }

                    Response.Redirect(CommonHelper.GetStoreLocation() + "privatemessages.aspx?tab=sent");
                } catch (Exception exc) {
                    this.LogService.InsertLog(LogTypeEnum.CustomerError, exc.Message, exc);
                }
            }
        }
    }
}